package org.gdstash.db;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class DBConstellationStar implements Comparable<DBConstellationStar> {
    private static final String TABLE_NAME = "GDC_CONSTELLATION_STAR";
    private static final int ROW_CONSTELLATION_ID = 1;
    private static final int ROW_INDEX = 2;
    private static final int ROW_BUTTON_ID = 3;
    private String constellationID;
    private int index;
    private String buttonID;
    private DBSkillButton dbButton;

    @Override
    public boolean equals(final Object o) {
        if (o == null) {
            return false;
        }
        if (!o.getClass().equals(DBConstellationStar.class)) {
            return false;
        }
        final DBConstellationStar star = (DBConstellationStar) o;
        return star.constellationID.equals(this.constellationID);
    }

    @Override
    public int hashCode() {
        int hash = this.constellationID.hashCode();
        hash += this.index;
        return hash;
    }

    @Override
    public int compareTo(final DBConstellationStar o) {
        if (!o.getClass().equals(DBConstellationStar.class)) {
            final Object[] args = {DBConstellationStar.class, o.toString()};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_NOT_OF_TYPE", args);
            throw new ClassCastException(msg);
        }
        final DBConstellationStar star = o;
        final int comp = this.constellationID.compareTo(star.constellationID);
        if (comp != 0) {
            return comp;
        }
        return this.index - star.index;
    }

    public String getButtonID() {
        return this.buttonID;
    }

    public void setButtonID(final String buttonID) {
        this.buttonID = buttonID;
    }

    public int getIndex() {
        return this.index;
    }

    public void setIndex(final int index) {
        this.index = index;
    }

    public DBSkillButton getButton() {
        return this.dbButton;
    }

    public DBSkill getSkill() {
        if (this.dbButton == null) {
            return null;
        }
        return this.dbButton.getSkill();
    }

    public static void createTable() throws SQLException {
        final String dropTable = "DROP TABLE GDC_CONSTELLATION_STAR";
        final String createTable = "CREATE TABLE GDC_CONSTELLATION_STAR (CONSTELLATION_ID VARCHAR(256) NOT NULL, INDEX            INTEGER, BUTTON_ID        VARCHAR(256), PRIMARY KEY (CONSTELLATION_ID, INDEX))";
        try (final Connection conn = GDDBData.getConnection()) {
            final boolean auto = conn.getAutoCommit();
            conn.setAutoCommit(false);
            try (final Statement st = conn.createStatement()) {
                if (GDDBUtil.tableExists(conn, "GDC_CONSTELLATION_STAR")) {
                    st.execute(dropTable);
                }
                st.execute(createTable);
                st.close();
                conn.commit();
            } catch (SQLException ex) {
                conn.rollback();
                throw ex;
            } finally {
                conn.setAutoCommit(auto);
            }
        }
    }

    public static void delete(final Connection conn, final String constellationID) throws SQLException {
        final String deleteEntry = "DELETE FROM GDC_CONSTELLATION_STAR WHERE CONSTELLATION_ID = ?";
        try (final PreparedStatement ps = conn.prepareStatement(deleteEntry)) {
            ps.setString(1, constellationID);
            ps.executeUpdate();
            ps.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static void insert(final Connection conn, final DBConstellation dbConstellation) throws SQLException {
        if (dbConstellation.getStarList() == null) {
            return;
        }
        if (dbConstellation.getStarList().isEmpty()) {
            return;
        }
        final String insert = "INSERT INTO GDC_CONSTELLATION_STAR VALUES (?,?,?)";
        try (final PreparedStatement ps = conn.prepareStatement(insert)) {
            for (final DBConstellationStar dbStar : dbConstellation.getStarList()) {
                if (dbStar.buttonID == null) {
                    continue;
                }
                if (dbStar.buttonID.isEmpty()) {
                    continue;
                }
                ps.setString(1, dbConstellation.getConstellationID());
                ps.setInt(2, dbStar.index);
                ps.setString(3, dbStar.buttonID);
                ps.executeUpdate();
                ps.clearParameters();
            }
            ps.close();
            conn.commit();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public static List<DBConstellationStar> getByConstellationID(final String constellationID) {
        List<DBConstellationStar> list = null;
        final String command = "SELECT * FROM GDC_CONSTELLATION_STAR WHERE CONSTELLATION_ID = ?";
        try (final Connection conn = GDDBData.getConnection();
             final PreparedStatement ps = conn.prepareStatement(command)) {
            ps.setString(1, constellationID);
            try (final ResultSet rs = ps.executeQuery()) {
                list = wrap(rs);
                conn.commit();
            } catch (SQLException ex) {
                throw ex;
            }
        } catch (SQLException ex2) {
            final Object[] args = {constellationID, "GDC_CONSTELLATION_STAR"};
            final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_READ_TABLE_BY_ID", args);
            GDMsgLogger.addError(msg);
            GDMsgLogger.addError(ex2);
        }
        return list;
    }

    private static List<DBConstellationStar> wrap(final ResultSet rs) throws SQLException {
        final LinkedList<DBConstellationStar> list = new LinkedList<DBConstellationStar>();
        while (rs.next()) {
            final DBConstellationStar star = new DBConstellationStar();
            star.constellationID = rs.getString(1);
            star.index = rs.getInt(2);
            star.buttonID = rs.getString(3);
            if (star.buttonID != null) {
                star.dbButton = DBSkillButton.getByButtonID(star.buttonID);
            }
            list.add(star);
        }
        return list;
    }
}
